package com.sysman.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2018年1月15日 上午11:47:58 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/SetRate")
public class SetRateController {
	/**
	 * 利率设置初始化界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/setrateshow")
	public ModelAndView setrateshow()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/SetRate/SetRate");
		return model;
	}
	/**
	 * 获取当前利率
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loadrate")
	public @ResponseBody Map<String,Object> loadrate(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid;
		Map<String,Object> result = new HashMap<>();
		String code="";
		String msg="";
		String docfefundrate="";
		String hospfefundrate="";
		try {
			JSONObject json = HttpRequest.sendPost("getrefundrate", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			if(json.get("data").toString()!=null) {
				try {
					JSONObject data = json.getJSONObject("data");
					docfefundrate=data.get("docfefundrate").toString();
					hospfefundrate=data.get("hospfefundrate").toString();
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错了，错误信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("docfefundrate", docfefundrate);
		result.put("hospfefundrate", hospfefundrate);
		
		return result;
	}
	/**
	 * 更新利率
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updatefundrate")
	public @ResponseBody Map<String,Object> updatefundrate(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String docfefundrate = request.getParameter("docfefundrate");
		String hospfefundrate = request.getParameter("hospfefundrate");
		String param = "adminloginid="+adminloginid+"&docfefundrate="+docfefundrate+"&hospfefundrate="+hospfefundrate;
		Map<String,Object> result = new HashMap<>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("updaterefundrate", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
}
